#include <jni.h>
#include <stddef.h>
#include "Log.h"
#include "JavaCall.h"
#include "FFmpeg.h"

_JavaVM *javaVM = NULL;
JavaCallFunc *JavaCall = NULL;
FFmpegPlayer *FFmpeg = NULL;

extern "C"
JNIEXPORT void JNICALL
Java_com_example_libplayer_player_Player_Prepared(JNIEnv *env, jobject instance, jstring url_, jboolean isOnlyMusic) {
    const char *url = env->GetStringUTFChars(url_, 0);
    // TODO
    if(JavaCall == NULL)
    {
        JavaCall = new JavaCallFunc(javaVM, env, &instance);
    }
    if(FFmpeg == NULL)
    {
        FFmpeg = new FFmpegPlayer(JavaCall, url, isOnlyMusic);
        JavaCall->onLoad(THREAD_MAIN, true);
        FFmpeg->preparedFFmpeg();
    }
}


extern "C"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
    jint result = -1;
    javaVM = vm;
    JNIEnv* env;

    if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK)
    {
        if(LOG_SHOW)
        {
            LOGE("GetEnv failed!");
        }
        return result;
    }
    return JNI_VERSION_1_4;
}extern "C"
JNIEXPORT void JNICALL
Java_com_example_libplayer_player_Player_Start(JNIEnv *env, jobject instance) {

    // TODO
    if(FFmpeg != NULL)
    {
        FFmpeg->start();
    }

}

extern "C"
JNIEXPORT void JNICALL
Java_com_example_libplayer_player_Player_Stop(JNIEnv *env, jobject instance, bool exit) {
    // TODO
    if(FFmpeg != NULL)
    {
        FFmpeg->exitByUser = true;
        FFmpeg->release();
        delete(FFmpeg);
        FFmpeg = NULL;
        if(JavaCall != NULL)
        {
            JavaCall->release();
            JavaCall = NULL;
        }
        if(!exit)
        {
            jclass jlz = env->GetObjectClass(instance);
            jmethodID jmid_stop = env->GetMethodID(jlz, "onStopComplete", "()V");
            env->CallVoidMethod(instance, jmid_stop);
        }
    }

}extern "C"
JNIEXPORT void JNICALL
Java_com_example_libplayer_player_Player_Pause(JNIEnv *env, jobject instance) {

    // TODO
    if(FFmpeg != NULL)
    {
        FFmpeg->pause();
    }

}extern "C"
JNIEXPORT void JNICALL
Java_com_example_libplayer_player_Player_Resume(JNIEnv *env, jobject instance) {

    // TODO
    if(FFmpeg != NULL)
    {
        FFmpeg->resume();
    }

}extern "C"
JNIEXPORT void JNICALL
Java_com_example_libplayer_player_Player_Seek(JNIEnv *env, jobject instance, jint secds) {

    // TODO
    if(FFmpeg != NULL)
    {
        FFmpeg->seek(secds);
    }

}extern "C"
JNIEXPORT jint JNICALL
Java_com_example_libplayer_player_Player_GetDuration(JNIEnv *env, jobject instance) {

    // TODO
    if(FFmpeg != NULL)
    {
        return FFmpeg->getDuration();
    }
    return 0;

}extern "C"
JNIEXPORT jint JNICALL
Java_com_example_libplayer_player_Player_GetAudioChannels(JNIEnv *env, jobject instance) {

    if(FFmpeg != NULL)
    {
        return FFmpeg->getAudioChannels();
    }
    return 0;
}extern "C"
JNIEXPORT jint JNICALL
Java_com_example_libplayer_player_Player_GetVideoWidth(JNIEnv *env, jobject instance) {

    // TODO
    if(FFmpeg != NULL)
    {
        FFmpeg->getVideoWidth();
    }

}extern "C"
JNIEXPORT jint JNICALL
Java_com_example_libplayer_player_Player_GetVideoHeidht(JNIEnv *env, jobject instance) {

    // TODO
    if(FFmpeg != NULL)
    {
        FFmpeg->getVideoHeight();
    }

}extern "C"
JNIEXPORT void JNICALL
Java_com_example_libplayer_player_Player_SetAudioChannels(JNIEnv *env, jobject instance, jint index) {

    // TODO
    if(FFmpeg != NULL)
    {
        FFmpeg->setAudioChannel(index);
    }

}